/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.spider.rule.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.spider.rule.domain.SpiderRule;
import me.zhengjie.spider.rule.service.SpiderRuleService;
import me.zhengjie.spider.rule.service.dto.SpiderRuleQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.spider.rule.service.dto.SpiderRuleDto;

/**
* @website https://eladmin.vip
* @author 雨来
* @date 2023-07-07
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "爬虫规则管理")
@RequestMapping("/api/spiderRule")
public class SpiderRuleController {

    private final SpiderRuleService spiderRuleService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('spiderRule:list')")
    public void exportSpiderRule(HttpServletResponse response, SpiderRuleQueryCriteria criteria) throws IOException {
        spiderRuleService.download(spiderRuleService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询爬虫规则")
    @ApiOperation("查询爬虫规则")
    @PreAuthorize("@el.check('spiderRule:list')")
    public ResponseEntity<PageResult<SpiderRuleDto>> querySpiderRule(SpiderRuleQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(spiderRuleService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增爬虫规则")
    @ApiOperation("新增爬虫规则")
    @PreAuthorize("@el.check('spiderRule:add')")
    public ResponseEntity<Object> createSpiderRule(@Validated @RequestBody SpiderRule resources){
        Boolean createFlag = spiderRuleService.create(resources);
        return new ResponseEntity<>(createFlag ? HttpStatus.CREATED : HttpStatus.CONFLICT);
    }

    @PutMapping
    @Log("修改爬虫规则")
    @ApiOperation("修改爬虫规则")
    @PreAuthorize("@el.check('spiderRule:edit')")
    public ResponseEntity<Object> updateSpiderRule(@Validated @RequestBody SpiderRule resources){
        Boolean updateFlag = spiderRuleService.update(resources);
        return new ResponseEntity<>(updateFlag ? HttpStatus.CREATED : HttpStatus.CONFLICT);
    }

    @DeleteMapping
    @Log("删除爬虫规则")
    @ApiOperation("删除爬虫规则")
    @PreAuthorize("@el.check('spiderRule:del')")
    public ResponseEntity<Object> deleteSpiderRule(@RequestBody Long[] ids) {
        spiderRuleService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}